package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.PurchaseListService;
import com.atguigu.jxc.service.ReturnListService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author Sanmier
 * @create 2020-12-29 19:41
 */
@RestController
@RequestMapping("returnListGoods")
public class ReturnListController {


    @Autowired
    private ReturnListService returnListService;

    @Autowired
    private Gson gson;

    //http://localhost:8080/returnListGoods/save?purchaseNumber=JH1605768306735
    //新增退货单
    @PostMapping("save")
    public ServiceVO save(@RequestParam("returnNumber") String returnNumber,
                          ReturnList returnList,
                          String returnListGoodsStr,
                          String supplierName,
                          HttpSession session){
        User currentUser = (User) session.getAttribute("currentUser");
        Integer userId = currentUser.getUserId();

        List<ReturnListGoods> returnListGoodsList = gson.fromJson(returnListGoodsStr, new TypeToken<List<ReturnListGoods>>() {}.getType());

        returnList.setUserId(userId);
        returnList.setReturnNumber(returnNumber);
        returnList.setSupplierName(supplierName);

        return returnListService.returnListGoodsListSave(returnList,returnListGoodsList);
    }


    //条件查询
    //http://localhost:8080/returnListGoods/list
    @PostMapping("list")
    public Map<String,Object> list(@Param("returnNumber") String returnNumber,
                                   @Param("supplierId") Integer supplierId,
                                   @Param("state") Integer state,
                                   @Param("sTime") String sTime,
                                   @Param("eTime") String eTime){

        return returnListService.getReSearchList(returnNumber,supplierId,state,sTime,eTime);
    }


    //查询退货单商品信息
    //http://localhost:8080/returnListGoods/goodsList
    @PostMapping("goodsList")
    public Map<String,Object> goodsList(@Param("returnListId") Integer returnListId){

        return returnListService.getReGoodsList(returnListId);
    }


    //http://localhost:8080/returnListGoods/delete
    //退货单删除
    @PostMapping("delete")
    public ServiceVO delete(@Param("returnListId") Integer returnListId){
        return returnListService.deleteReturnListGoods(returnListId);
    }


    //支付结算（修改退货单付款状态）
    //http://localhost:8080/returnListGoods/updateState
    @PostMapping("updateState")
    public ServiceVO updateState(@Param("returnListId") Integer returnListId){
        return returnListService.updateReturneListGoods(returnListId);
    }
}
